/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  ActivityAbstractHelper.java                                      
 *  File Contents Outline :  活动抽象持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbActivityAbstractRule;
import storage.util.StorageManager;

/**
 *
 * @author wan
 */
public class ActivityAbstractHelper {

    /**
     * 将清洗规则保存到LaUDMS数据库中
     *
     * @param activityAbstractRule 要存储的模型对象
     */
    public void saveActivityAbstractRule(DbActivityAbstractRule activityAbstractRule) {
       // StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_activityAbstractRule);
          PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_activityAbstractRule"));
//        sm.open();
        sm.setRowKey(activityAbstractRule.getRowKey());
        sm.put(DbActivityAbstractRule.cl_name, activityAbstractRule.getName());
        sm.put(DbActivityAbstractRule.cl_createTime, activityAbstractRule.getCreateTime());
        sm.put(DbActivityAbstractRule.cl_product, activityAbstractRule.getProduct());
        sm.put(DbActivityAbstractRule.cl_tagList, activityAbstractRule.getTaglist());
        ArrayList<String> abstractRuleList = activityAbstractRule.getAbstractRuleList();
        for (int i = 0; i < abstractRuleList.size(); i++) {
            sm.put(DbActivityAbstractRule.cl_activityAbstractRule + "_" + i, abstractRuleList.get(i));
        }
//        sm.close();
    }

    /**
     * 
     * @param activityAbstractRule
     * @param ruleValue 
     */
    public void insertNewAbstractionRule(DbActivityAbstractRule activityAbstractRule, final String ruleValue) {       
       PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_activityAbstractRule"));
        sm.setRowKey(activityAbstractRule.getRowKey());
        int ruleLength=activityAbstractRule.getAbstractRuleList().size();
        sm.put(DbActivityAbstractRule.cl_activityAbstractRule+"_"+ruleLength, ruleValue);
    }

    /**
     * 
     * @param columnName
     * @param value
     * @return
     */
    public List<DbActivityAbstractRule> getActivityAbstractRules(final String columnName, final String value) {
        List<DbActivityAbstractRule> result = new LinkedList<DbActivityAbstractRule>();
          PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_activityAbstractRule"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, createTime = null, product = null, taglist = null;
                ArrayList<String> abstractRuleList = new ArrayList<String>();
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbActivityAbstractRule.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbActivityAbstractRule.cl_tagList)) {
                        taglist = columnsValue;
                    } else if (columnsName.equals(DbActivityAbstractRule.cl_createTime)) {
                        createTime = columnsValue;
                    } else if(columnsName.equals(DbActivityAbstractRule.cl_product)){
                        product=columnsValue;
                    } else if(columnsName.startsWith(DbActivityAbstractRule.cl_activityAbstractRule)){
                        abstractRuleList.add(columnsValue);
                    }                    
                }
                DbActivityAbstractRule pActivityAbstractRule = new DbActivityAbstractRule(rowKey, name, product, taglist, abstractRuleList,createTime);
                result.add(pActivityAbstractRule);
            }
        } catch (Exception ex) {
            Logger.getLogger(ActivityAbstractHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public ArrayList<String> getAllActivityAbstractRuleName() {
          PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_activityAbstractRule"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> activityAbstractRuleNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = "";
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbActivityAbstractRule.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!activityAbstractRuleNameList.contains(name) && name.length() > 0) {
                    activityAbstractRuleNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(ActivityAbstractHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return activityAbstractRuleNameList;
    }

    public void deleteActivityAbstractRule(final String rowKey) {
        PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_activityAbstractRule"));
        sm.delete(rowKey);
//        sm.close();
    }
}
